//
//  RemindMeApp.m
//  RemindMe
//
//  Created by Siavash Ghamaty on 9/25/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "RemindMeApp.h"

NSString *const kStartButtonTitle	= @"START";
NSString *const kStopButtonTitle	= @"STOP";

@implementation RemindMeApp

- (void)awakeFromNib
{
	[GrowlApplicationBridge setGrowlDelegate:self];
}

- (void) updateTimer
{
	if( [secondsInput intValue] > 0 ) {
		[secondsInput setStringValue: [NSString stringWithFormat: @"%d", [secondsInput intValue]-1 ] ];
		if( [secondsInput intValue] == 0 ) {
			
			// Creating growl notification
			[GrowlApplicationBridge
			 notifyWithTitle:@"A reminder from RemindMe App"
			 description:@"Your timer has fired"
			 notificationName:@"StandardReminder"
			 iconData: nil
			 priority:1
			 isSticky:NO
			 clickContext:@"test"];

		}
	}
	
}

- (void) invalidateTimer
{
	if( [timer isValid] ) {
		[timer invalidate];
	}
	
}
		
- (IBAction) startOrStopGrowlTimer: (id) sender
{	
	// Toggle the button title
	NSString *buttonTitle = ( [startStopButton title] == kStopButtonTitle ) ? kStartButtonTitle : kStopButtonTitle;
	
	// Using button title to invalidate timer or schedule one
	if( buttonTitle == kStopButtonTitle ) {
		timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];		
	} else {
		[self invalidateTimer];
	}
	
	[startStopButton setTitle:buttonTitle];
	
}



#pragma mark ---   Growl Delegate Methods   ---

- (void) growlNotificationWasClicked:(id)clickContext
{
	[self activateIgnoringOtherApps:YES];
}

- (NSDictionary *) registrationDictionaryForGrowl
{
	NSArray *notifications = [NSArray arrayWithObject: @"StandardReminder"];
	NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
			notifications, GROWL_NOTIFICATIONS_ALL,
			notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
	
	return dict;
}

@end
